package com.demandbreakdown.xiyun.model.vo.picture;

import cn.hutool.json.JSONUtil;
import com.demandbreakdown.xiyun.model.entity.Picture;
import com.demandbreakdown.xiyun.model.vo.user.UserVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 图片信息VO
 *
 * @author poxz
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "图片信息VO")
public class PictureVO {
    @Schema(description = "图片ID", example = "123")
    private Long id;

    @Schema(description = "图片访问URL", example = "https://oss/xxx.jpg")
    private String fileUrl;

    @Schema(description = "图片名称", example = "美丽的风景")
    private String pictureName;

    @Schema(description = "原始文件名", example = "image.jpg")
    private String originalName;

    @Schema(description = "图片简介", example = "这是一张美丽的风景图片")
    private String introduction;

    @Schema(description = "图片分类", example = "风景")
    private String category;

    @Schema(description = "标签列表", example = "[\"旅行\", \"天空\"]")
    private List<String> tags;

    @Schema(description = "文件大小（字节）", example = "1048576")
    private Long fileSize;

    @Schema(description = "图片宽度（像素）", example = "1920")
    private Integer picWidth;

    @Schema(description = "图片高度（像素）", example = "1080")
    private Integer picHeight;

    @Schema(description = "图片格式", example = "jpg")
    private String picFormat;

    @Schema(description = "审核状态：0-待审核，1-已通过，2-已拒绝", example = "1")
    private Integer reviewStatus;

    @Schema(description = "审核信息", example = "审核通过")
    private String reviewMessage;

    @Schema(description = "审核人ID", example = "456")
    private Long reviewerId;

    @Schema(description = "审核时间", example = "2025-09-20T12:30:00Z")
    private LocalDateTime reviewTime;

    @Schema(description = "浏览次数", example = "100")
    private Integer viewCount;

    @Schema(description = "下载次数", example = "50")
    private Integer downloadCount;

    @Schema(description = "分享次数", example = "20")
    private Integer shareCount;

    @Schema(description = "缩略图URL", example = "https://oss/xxx_thumb.jpg")
    private String thumbnailUrl;

    @Schema(description = "上传用户ID", example = "789")
    private Long userId;

    @Schema(description = "所属空间ID", example = "123")
    private Long spaceId;

    @Schema(description = "上传用户信息")
    private UserVO user;

    @Schema(description = "创建时间", example = "2025-09-20T12:00:00Z")
    private LocalDateTime createTime;

    public static PictureVO from(Picture picture) {
        if (picture == null) {
            return null;
        }

        PictureVO pictureVO = PictureVO.builder()
                .id(picture.getId())
                .fileUrl(picture.getFileUrl())
                .pictureName(picture.getPictureName())
                .originalName(picture.getOriginalName())
                .introduction(picture.getIntroduction())
                .category(picture.getCategory())
                .fileSize(picture.getFileSize())
                .picWidth(picture.getPicWidth())
                .picHeight(picture.getPicHeight())
                .picFormat(picture.getPicFormat())
                .reviewStatus(picture.getReviewStatus())
                .reviewMessage(picture.getReviewMessage())
                .reviewerId(picture.getReviewerId())
                .reviewTime(picture.getReviewTime())
                .viewCount(picture.getViewCount())
                .downloadCount(picture.getDownloadCount())
                .shareCount(picture.getShareCount())
                .thumbnailUrl(picture.getThumbnailUrl())
                .userId(picture.getUserId())
                .spaceId(picture.getSpaceId())
                .createTime(picture.getCreateTime())
                .build();

        // 解析tags字段（从JSON字符串转为List）
        if (picture.getTags() != null && !picture.getTags().isEmpty()) {
            try {
                List<String> tags = JSONUtil.toList(picture.getTags(), String.class);
                pictureVO.setTags(tags);
            } catch (Exception e) {
                // 解析失败则忽略
            }
        }

        return pictureVO;
    }

    public static PictureVO from(Picture picture, UserVO user) {
        PictureVO pictureVO = from(picture);
        if (pictureVO != null) {
            pictureVO.setUser(user);
        }
        return pictureVO;
    }
}